package bb.lanxing.network;

import com.baidu.mapapi.model.LatLng;
import com.umeng.analytics.pro.d;
import gov.nist.core.Separators;

import bb.lanxing.manager.SharedManager;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Log;

import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class GoogleClient {
    public static String AltitudeApiHost = "http://woc.bluebank.com.cn/api/google_elevation";

    public static final String TAG = "HttpClient";
    private static String googleAltitudeUrl = "http://{0}/maps/api/elevation/json";

    private static String googleStaticMapUrl = "http://{0}/maps/api/staticmap";
    public static final OkHttpClient client = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).build();

    static String buildUrlByParams(String str, HashMap<String, Object> hashMap) {
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        if (hashMap != null) {
            Iterator<String> it = hashMap.keySet().iterator();
            if (it.hasNext()) {
                sb.append(Separators.QUESTION);
            }
            while (it.hasNext()) {
                try {
                    String next = it.next();
                    Object obj = hashMap.get(next);
                    sb.append(next);
                    sb.append(Separators.EQUALS);
                    sb.append(StandardCharsets.UTF_8);//URLEncoder.encode(obj.toString(), r.b)
                    sb.append("&");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static void requestAltitude(Callback callback, LatLng latLng, boolean z) {
        String str;
        HashMap hashMap = new HashMap();
//        hashMap.put(an.ac, "false");
        hashMap.put(d.B, latLng.latitude + "," + latLng.longitude);
        if (z) {
            str = MessageFormat.format(googleAltitudeUrl, SharedManager.getInstance().getGoogleHost());
        } else {
            str = AltitudeApiHost;
        }
        Request build = new Request.Builder()
                .addHeader("Accept", "application/json")
                .url(buildUrlByParams(str, hashMap))
                .build();
        Log.d("HttpClient", build.toString());
        client.newCall(build).enqueue(callback);
    }

    public static void requestStaticMap(Callback callback, String str, List<LatLng> list) {
        String javaEncodingString = CommonUtil.getJavaEncodingString(str);
        StringBuilder sb = new StringBuilder();
        sb.append(MessageFormat.format(googleStaticMapUrl, SharedManager.getInstance().getGoogleHost()));
        sb.append(Separators.QUESTION);
        sb.append("size=640x280");
        sb.append("&path=weight%3a5%7Ccolor%3a0xe51c23aa%7Cenc%3a" + javaEncodingString);
        StringBuffer stringBuffer = new StringBuffer();
        if (list != null && list.size() > 0) {
            LatLng latLng = list.get(0);
            LatLng latLng2 = list.get(list.size() - 1);
            stringBuffer.append(String.format("&markers=icon:http://static.bluebank.com.cn/start-1x.png|%f,%f&markers=icon:http://static.bluebank.com.cn/end-1x.png|%f,%f", latLng.latitude, latLng.longitude, latLng2.latitude, latLng2.longitude));
            if (list.size() > 2) {
                for (int i = 1; i < list.size() - 1; i++) {
                    LatLng latLng3 = list.get(i);
                    stringBuffer.append(String.format("&markers=icon:http://static.bluebank.com.cn/point-1x.png|%f,%f", latLng3.latitude, latLng3.longitude));
                }
            }
        }
        sb.append(CommonUtil.getJavaEncodingString(stringBuffer.toString()));
        try {
            Request build = new Request.Builder().url(sb.toString()).build();
            Log.d("HttpClient", build.toString());
            client.newCall(build).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
